function [ EC ] = APEaddDose(datasetname, doseIn)

% [ EC ] = APEaddDose(datasetname, doseIn)
%
%   allows to add a new variable containing X-ray dose received by
%           the sample. most usefull for kinetic data.
%
%   INPUT       datasetname     : .mat file to process
%               [doseIn]        : avoid GUI and enter dose directly for
%                                  single spectrum [optional]
%
%   OUTPUT      EC              :   Error Code (0 = OK, 1 = Error, 2 = Aborted)

%  (c) F. Dworkowski, 2012-14
%      Version 1.1, 2014-01-27
%      Version 1.1, 2013-12-19
%      Version 1.0, 2012-03-19

%% ===== CONFIG & INITILIZE =====
EC=1;
if nargin < 1
    help APEadddose
    return 
end

options.WindowStyle='normal';       % Make GUI Windows non-modal

%% ===== CODE =====
try dataset = load(datasetname);
catch ME
  errordlg({ME.message;'';'File not found.'});
  return
end

% check if more than one spectrum
lengthOfData = length(dataset.datay(1,:));
if lengthOfData> 1
    constScaleYN= questdlg({'Is the dose between spectra' ...
                    'constant or variable?'}, ...
                    'Dose type','Constant','Variable','Cancel','Constant');
    switch constScaleYN
        case 'Constant'
            prompt      = {'Dose per spectrum?', ...
                            'Initial dose? [0]',...
                            'Modifier (e.g. 10^6)'};
            def         = {'','0','1e6'};
            doseInput   = inputdlg(prompt,'Enter Dose',1,def,options);
            singleDose  = str2double(char(doseInput(1)));
            initialDose = str2double(char(doseInput(2)));
            modifyDose  = str2double(char(doseInput(3)));
            dose = (initialDose*modifyDose):(singleDose*modifyDose):((initialDose*modifyDose)+((lengthOfData-1)*(singleDose*modifyDose)));
                
        case 'Variable'
            totalDose = 0;
            fromVariableYN= questdlg({'Calculate from other variable' ...
                    'or input dose(s) manually?'}, ...
                    'Variable type','From Variable','Manual','Cancel','Manual');
            switch fromVariableYN
                case 'From Variable'
                    str = fieldnames(dataset);
                    [s] = listdlg('PromptString','Select dependend variable:',...
                        'SelectionMode','single',...
                        'ListString',str);
                    eval(['tempvariable = dataset.',str{s},';']);
                    [x] = inputdlg('Enter conversion factor', 'Conversion Factor',1,{''},options);
                    factor = str2num(x{:});
                    dose = tempvariable * factor;

                case 'Manual'
                    for i = 1:lengthOfData
                        prompt = ['What is the dose for spectrum number ',num2str(i),' ?'];
                        answer = char(inputdlg(prompt,'Enter Dose'));
                        answer2 = [answer,'x'];              % avoid MATLAB bug to not compare empty CHAR
                        if answer2 == 'x'
                            disp('User selected Cancel')
                            EC = 2;
                            return
                        else
                            singleDose = str2double(answer);
                            totalDose = totalDose + singleDose;
                            dose(i) = totalDose;
                        end
                    end
            end
        case 'Cancel'
           disp('User selected Cancel')
           EC = 2;
           return
    end

else
    if nargin < 2
        prompt = 'Enter the dose received by this sample';
        dose = str2double(char(inputdlg(prompt,'Enter Dose')));
    else
        dose = doseIn;
    end
end

%% ===== CLOSING =====

dataset.dose = dose;
save(datasetname,'-struct','dataset')

EC = 0;